package mobi.greenplate.client.converter;

import mobi.greenplate.client.domain.Address;

/**
 *
 * @author dmakariev
 */
public class AddressConverter {

    private static final String ADDRESS_TAG="address";
    private static final String ID = "id";
    private static final String CITY = "city";
    private static final String LATITUDE = "latitude";
    private static final String LONGTITUDE = "longtitude";
    private static final String STATE = "state";
    private static final String STREET = "street";
    private static final String ZIP = "zip";

    public static Address endTag(Address address, String tagName, String text) {
        if (tagName.equalsIgnoreCase(ID)) {
            address.setId(Long.valueOf(text));
        } else if (tagName.equalsIgnoreCase(CITY)) {
            address.setCity(text);
        } else if (tagName.equalsIgnoreCase(LATITUDE)) {
            address.setLatitude(Long.valueOf(text));
        } else if (tagName.equalsIgnoreCase(LONGTITUDE)) {
            address.setLongtitude(Long.valueOf(text));
        } else if (tagName.equalsIgnoreCase(STATE)) {
            address.setState(text);
        } else if (tagName.equalsIgnoreCase(STREET)) {
            address.setStreet(text);
        } else if (tagName.equalsIgnoreCase(ZIP)) {
            address.setZip(text);
        }
        return address;
    }    

    public static String toXml(Address address){
        final XmlBuilder xmlBuilder = XmlBuilder.newRoot(ADDRESS_TAG);
        appendToXmlBuilder(xmlBuilder, address);
        return xmlBuilder.buildXml();
    }

    private static void appendToXmlBuilder(XmlBuilder builder, Address address) {
        if (null != address.getId()) {
            builder.addTag(ID, String.valueOf(address.getId()));
        }

        builder.addTag(CITY, address.getCity()).
                addTag(LATITUDE, String.valueOf(address.getLatitude())).
                addTag(LONGTITUDE, String.valueOf(address.getLongtitude())).
                addTag(STATE, address.getState()).
                addTag(STREET, address.getStreet()).
                addTag(ZIP, address.getZip());
    }

}
